package com.wwf.sqlsession;

import com.wwf.pojo.Configuration;
import com.wwf.pojo.MappedStatement;
import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession{
    private Configuration configuration;
    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementid, Object... parameters) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        SqlExecutor simpleSqlExecutor = new SimpleSqlExecutor();
        MappedStatement mappedStatement = configuration.getMappedSatements().get(statementid);
        List<Object> list = simpleSqlExecutor.query(configuration, mappedStatement, parameters);
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementid, Object... parameters) throws IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        List<Object> objects = selectList(statementid, parameters);
        if(objects !=null && objects.size()==1) return (T) objects.get(0);
        return null;
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className+"."+methodName;
                Type genericReturnType = method.getGenericReturnType();
                MappedStatement mappedStatement = configuration.getMappedSatements().get(statementId);
                String sqlTypeStr = mappedStatement.getSqlType();
                SqlType sqlType = SqlType.valueOf(sqlTypeStr);
                Object object = null;
                switch (sqlType){
                    case select:
                        //如果设置了泛型返回值
                        if(genericReturnType instanceof ParameterizedType){
                            object = selectList(statementId, args);
                        }else{
                            object =  selectOne(statementId,args);
                        }
                        break;
                    case insert:
                        object = insert(statementId, args);
                        break;
                    case update:
                        object = update(statementId, args);
                        break;
                    case delete:
                        object = delete(statementId, args);
                        break;
                }

                return object;

            }
        });
        return (T) proxyInstance;
    }

    @Override
    public int insert(String statementid, Object... parameters) throws ClassNotFoundException, SQLException, IllegalAccessException, NoSuchFieldException {
        SqlExecutor simpleSqlExecutor = new SimpleSqlExecutor();
        MappedStatement mappedStatement = configuration.getMappedSatements().get(statementid);
         return simpleSqlExecutor.insert(configuration,mappedStatement,parameters);

    }

    @Override
    public int update(String statementid, Object... parameters) throws ClassNotFoundException, SQLException, IllegalAccessException, NoSuchFieldException {
        SqlExecutor simpleSqlExecutor = new SimpleSqlExecutor();
        MappedStatement mappedStatement = configuration.getMappedSatements().get(statementid);
        return simpleSqlExecutor.update(configuration,mappedStatement,parameters);
    }

    @Override
    public int delete(String statementid, Object... parameters) throws ClassNotFoundException, SQLException, IllegalAccessException, NoSuchFieldException {
        SqlExecutor simpleSqlExecutor = new SimpleSqlExecutor();
        MappedStatement mappedStatement = configuration.getMappedSatements().get(statementid);
        return simpleSqlExecutor.deleteById(configuration,mappedStatement,parameters);
    }
}
